package sgi.client;

import java.io.IOException;
import java.util.Iterator;

import sgi.details_new.AccountInfoDetails;
import sgi.details_new.TariffDetails;
import sgi.message.MessageTariffExtract;
import sgi.message.MessageViewInfo;
import sgi.user.User;

public class SGIViewTariffController extends SGIController {

	public static SGIViewTariffController tariffInfo;
	public static SGIViewTariffUI tariffInfoUI;
	private static User user;
	
	
	/** create controller */
	public static SGIViewTariffController create() throws IOException
	{
		if(SGIViewTariffController.tariffInfo == null)
		{
			SGIViewTariffController.tariffInfo = new SGIViewTariffController();
			tariffInfoUI.setSGIViewTariffController(SGIViewTariffController.tariffInfo);
		}
		SGIViewTariffController.tariffInfoUI.setVisible(true);
		return tariffInfo;
	}
	/** constructor */
	private SGIViewTariffController() 
	{
		SGIViewTariffController.tariffInfoUI = new SGIViewTariffUI();
	}
	/** message for viewing info */
	public void ViewInfo()
	{
		MessageTariffExtract msg = new MessageTariffExtract();
		sendViewInfo(msg);
	}
	/** sending message */
	private void sendViewInfo(MessageTariffExtract msg)
	{
		try {
			(SGIClientConnectionController.createConnection()).handleMessageFromClientUI(msg);
		} catch  (IOException e) {e.printStackTrace();}
	}
	/** terminating ui */
	public static void terminate()
	{
		SGIViewTariffController.tariffInfoUI.setVisible(false);
		SGIViewTariffController.tariffInfoUI=null;
		SGIViewTariffController.tariffInfo=null;
		
	}
	
	/** handle message to get info of tariff */
	void handleMessage(Object msg) {
		Iterator<TariffDetails> iterator = (((MessageTariffExtract)msg).getTariffInfo()).iterator();
		SGIViewTariffController.tariffInfoUI.addTariffInfo(iterator.next());

	}
	/** get ui */
	public static SGIViewTariffUI getUI()
	{
		return tariffInfoUI;
	}

}
